package com.example.epidemicsurveillance.utils.spider.processor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.epidemicsurveillance.entity.Article;
import com.example.epidemicsurveillance.service.IArticleService;
import com.example.epidemicsurveillance.utils.rabbitmq.EmailSendUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName NotificationProcessor
 * @Author 朱云飞
 * @Date 2021/10/17 16:25
 * @Version 1.0
 **/
@Component
public class NotificationProcessor implements PageProcessor {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private EmailSendUtil emailSendUtil;

    private Integer count;

    @Override
    public void process(Page page) {
        List<Selectable> selectableList = page.getHtml().css("div.menu1").links().regex(".*htm$").nodes();
        List<String> urlList=new LinkedList<>();
        if(selectableList.size() != 0) {
            //需要爬取的页面
            for (int i = 0; i <60 && i<selectableList.size(); i++) {
                urlList.add(selectableList.get(i).get());
            }
            count=0;
            page.addTargetRequests(urlList);
        }else {
            String title=page.getHtml().css("title").toString().replace("<title>","").replace("</title>","")
                    .replaceAll("_部门政务_中国政府网","");
            String content=page.getHtml().css("div.pages_content").toString() .replaceAll("<img","<img style=\"width: 85%;\"");
            String url=page.getUrl().toString();
            Integer sort=Integer.valueOf(url.substring(url.indexOf('_')+1,url.indexOf("htm")-1));
            String time = LocalDateTime.now().toString();
            Article article=new Article();
            article.setTitle(title);
            article.setContent(content);
            article.setUrl(url);
            article.setTime(time);
            article.setType(2);
            article.setSort(sort);
            QueryWrapper<Article> wrapper=new QueryWrapper<>();
            wrapper.eq("title",title);
            count++;
            System.out.println(count);
            if((articleService.getOne(wrapper) == null || title.length() > 0) && count <= 40){
                articleService.save(article);
            }
        }

    }

    private Site site = Site.me()
            .setCharset("utf-8") // 设置编码
            .setRetrySleepTime(3000) // 设置重试的间隔时间
            .setSleepTime(5); // 设置重试次数
    @Override
    public Site getSite() {
        return this.site;
    }
}
